//
//  MoreViewController.m
//  WXMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 无限互联. All rights reserved.
//

#import "MoreViewController.h"

@interface MoreViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    NSString *userName;
    UIImage *userIcon;
}
@property (weak, nonatomic) IBOutlet UILabel *sizeLabel;

@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getDataFromSandBox];
    //self.view.backgroundColor = [UIColor redColor];
    self.navigationController.title = @"更多";
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    self.tableView.separatorColor = kTableViewSeColor;
    //只能设置左右偏移self.tableView.separatorInset = UIEdgeInsetsMake(10, 15, 0, 0);
    // Do any additional setup after loading the view.
    NSLog(@"%@",NSHomeDirectory());
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
    headerView.backgroundColor = [UIColor clearColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(25, 25, 100, 100);
    [button setImage:userIcon forState:UIControlStateNormal];
    button.layer.cornerRadius = 50;
    //
    button.layer.masksToBounds = YES;
    button.tag = 2016;
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [headerView addSubview:button];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(150, 25, 200, 40)];
    label.textAlignment = NSTextAlignmentLeft;
    [label setFont:[UIFont systemFontOfSize:30]];
    label.textColor = [UIColor whiteColor];
    label.tag = 2017;
    label.text = userName;
    
    [headerView addSubview:label];
    
    self.tableView.tableHeaderView = headerView;
    
}

- (void)buttonAction:(UIButton *)button {
    
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    //弹出相册
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = sourceType;
    imagePicker.delegate = self;
    
    [self presentViewController:imagePicker animated:YES completion:NULL];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self readCatchSize];
}

- (void)readCatchSize {
    
    NSInteger fileSize = 0;
    
    NSString *catchPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"];

    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:catchPath];
 
    for (NSString *str in fileEnumerator) {
        NSString *filePath = [catchPath stringByAppendingPathComponent:str];
 
        NSDictionary *attDic = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        //NSLog(@"%@",attDic);
        fileSize += [attDic fileSize];
        
    }
    //NSLog(@"%li",fileSize);
    //NSInteger fileSize = [[SDImageCache sharedImageCache] getSize];
    
    self.sizeLabel.text = [NSString stringWithFormat:@"%.2f MB",fileSize / 1024.0 /1024.0];
}

- (void)removeCatch {
    NSString *catchPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"];
    [[NSFileManager defaultManager] removeItemAtPath:catchPath error:nil];
//    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:catchPath];
//    for (NSString *str in fileEnumerator) {
//        NSString *filePath = [catchPath stringByAppendingPathComponent:str];
//        
//        [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
//    }

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        self.sizeLabel.text = @"0.0MB";
        [self removeCatch];
        //[[SDImageCache sharedImageCache] clearDisk];
    }
}
#pragma mark 相册代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
    //获取需要设置的图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    UIButton *button = [self.tableView.tableHeaderView viewWithTag:2016];
    
    [button setImage:image forState:UIControlStateNormal];
    
    
    UILabel *label = [self.tableView.tableHeaderView viewWithTag:2017];
    userName = @"1313113";
    label.text = userName;
    
    [self saveDataToSandBox];
}

- (void)saveDataToSandBox
{
    if (userIcon != nil) {
        [[NSUserDefaults standardUserDefaults] setObject:userName forKey:@"userName"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSData *data = UIImageJPEGRepresentation(userIcon, 0.8);
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"userIcon"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
}

//从沙盒当中获取数据
- (void)getDataFromSandBox
{
    userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    userIcon = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"userIcon"]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
